package com.zx.onlineeducation.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zx.onlineeducation.service.edu.entity.TeacherDO;
import com.zx.onlineeducation.service.edu.entity.vo.TeacherQueryVO;
import com.zx.onlineeducation.service.edu.mapper.TeacherMapper;
import com.zx.onlineeducation.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author 郑雪
 * @since 2022-01-07
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, TeacherDO> implements TeacherService {

    /**
     * 查询讲师分页数据列表
     *
     * @param current        当前页
     * @param size           每页显示条数
     * @param teacherQueryVO 讲师查询对象 {@link TeacherQueryVO}
     * @return 分页对象 {@link Page} {@link TeacherDO}
     * @author 郑雪
     * @since 2022-01-25
     */
    @Override
    public Page<TeacherDO> page(Long current, Long size, TeacherQueryVO teacherQueryVO) {
        // 1.创建分页对象
        Page<TeacherDO> page = new Page<>(current, size);
        // 2.创建条件查询对象
        QueryWrapper<TeacherDO> queryWrapper = new QueryWrapper<>();
        String name = teacherQueryVO.getName();
        Integer level = teacherQueryVO.getLevel();
        String startTime = teacherQueryVO.getStartTime();
        String endTime = teacherQueryVO.getEndTime();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(startTime)) {
            queryWrapper.ge("create_time", startTime);
        }
        if (!StringUtils.isEmpty(endTime)) {
            queryWrapper.le("create_time", endTime);
        }
        // 3.执行分页查询
        this.page(page, queryWrapper);
        return page;
    }

    /**
     * 添加讲师
     *
     * @param teacherDO 讲师数据对象 {@link TeacherDO}
     * @return 成功true，失败false
     * @author 郑雪
     * @since 2022-01-25
     */
    // @CacheEvict 清空缓存
    @CacheEvict(value = "teacher", allEntries = true)
    @Override
    public boolean saveTeacher(TeacherDO teacherDO) {
        return this.save(teacherDO);
    }

    /**
     * 修改讲师
     *
     * @param teacherDO 讲师数据对象 {@link TeacherDO}
     * @return 成功true，失败false
     * @author 郑雪
     * @since 2022-01-25
     */
    // @CacheEvict 清空缓存
    @CacheEvict(value = "teacher", allEntries = true)
    @Override
    public boolean updateTeacherById(TeacherDO teacherDO) {
        return this.updateById(teacherDO);
    }

    /**
     * 删除讲师
     *
     * @param id 讲师ID
     * @return 成功true，失败false
     * @author 郑雪
     * @since 2022-01-25
     */
    // @CacheEvict 清空缓存
    @CacheEvict(value = "teacher", allEntries = true)
    @Override
    public boolean removeTeacherById(String id) {
        return this.removeById(id);
    }

    /**
     * 获取名师数据列表
     *
     * @param teacherLimit 名师显示条数
     * @return TeacherDO对象的List集合 {@link TeacherDO}
     * @author 郑雪
     * @since 2022-01-25
     */
    // @Cacheable 将返回结果进行缓存，以后查询数据，直接从缓存中取，key生成规则：value::key ，例如：teacher::selectIndexList
    @Cacheable(value = "teacher", key = "'selectIndexList'")
    @Override
    public List<TeacherDO> getFamousTeacherList(Integer teacherLimit) {
        QueryWrapper<TeacherDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit " + teacherLimit);
        return this.list(queryWrapper);
    }
}
